package com.sishu.njrtsdms.controller;

import com.sishu.njrtsdms.domain.core.Groups;
import com.sishu.njrtsdms.domain.core.R;
import com.sishu.njrtsdms.domain.dto.CollegeDTO;
import com.sishu.njrtsdms.domain.vo.CollegeVO;
import com.sishu.njrtsdms.service.CollegeService;

import org.jooq.types.ULong;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import lombok.extern.slf4j.Slf4j;

@RestController
@RequestMapping("/college")
@Slf4j
@Validated
public class CollegeController {

    private final CollegeService collegeService;

    public CollegeController(CollegeService collegeService) {
        this.collegeService = collegeService;
    }


    @GetMapping
    public R<List<CollegeVO>> list(CollegeDTO collegeDTO) {
        List<CollegeVO> collegeVOList = collegeService.listCollege(collegeDTO);
        return R.ok(collegeVOList);
    }

    /**
     * 新增学院
     */
    @PostMapping
    public R<String> addCollege(@Validated({Groups.Add.class})
                                @RequestBody
                                CollegeDTO collegeDTO) {
        collegeService.addCollege(collegeDTO);
        return R.ok();
    }

    /**
     * 修改学院
     */
    @PutMapping("/{collegeId}")
    public R<String> update(
            @PathVariable
            ULong collegeId,
            @Validated({Groups.Add.class})
            @RequestBody
            CollegeDTO collegeDTO) {
        collegeService.updateCollege(collegeId, collegeDTO);
        return R.ok();
    }


    @GetMapping("/{collegeId}")
    public R<CollegeVO> getCollege(@PathVariable(required = false, name = "collegeId")
                                   Long collegeId) {
        CollegeVO collegeVO = collegeService.queryOneCollegeByCollegeId(collegeId);
        return R.ok(collegeVO);
    }


}
